import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const author = (to, from, next) => {
    console.log(to);
    //守卫创建博客的路由
    //登录成功  username
    let username = localStorage.getItem('username') || '';
    if(username){
        //已经登录
        next();
    }else{
        //未登录
        next(`/login?prev=${to.path}`);
    }
}

const routes = [
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import(/* webpackChunkName: "search" */ '../views/search.vue')
  },
  {
    path: '/create_blog',
    name: 'create_blog',
    beforeEnter:author ,
    component: () => import(/* webpackChunkName: "create_blog" */ '../views/create_blog.vue')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import(/* webpackChunkName: "detail" */ '../views/detail.vue')
  },
  {
    path: '/my_create_blog',
    name: 'my_create_blog',
    beforeEnter:author ,
    component: () => import(/* webpackChunkName: "my_create_blog" */ '../views/my_create_blog.vue')
  },
  {
    path: '/my',
    name: 'my',
    component: () => import(/* webpackChunkName: "my" */ '../views/my.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login.vue')
  },
  {
    path: '/registry',
    name: 'registry',
    component: () => import(/* webpackChunkName: "registry" */ '../views/registry.vue')
  },
  {
      path:'/',
      redirect:'/home'
  }
  
];

/**
 * 路由守卫：
 * 
 * 1.全局守卫
 * 
 * 2.路由独享守卫
 * 
 * 3.组件内守卫
 * 
 * 
*/

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
